package com.dakor.catalog.gui;

import com.dakor.catalog.persistence.entity.Film;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Main frame of application.
 *
 * @author Korobeynikov Dmitry
 * @version 1.0
 */
public class CatalogForm extends JFrame {
    /** minimun width of frame */
    public static final int MINIMUM_WIDTH_SIZE = 600;

    /** minimum height of frame */
    public static final int MINIMUM_HEIGHT_SIZE = 400;

    /** the messages's provider */
    private static final ResourceBundle msgs =
            ResourceBundle.getBundle("i18n/CatalogFormMessages", new Locale("ru", "RU"));

    /** cell constraints */
    private static final CellConstraints CELL_CONSTRAINTS = new CellConstraints();

    /** the instance of class */
    private static CatalogForm INSTANCE;

    /** the table instance */
    private DefaultTableModel tableModel;

    /**
     * Private constructor.
     */
    private CatalogForm() {
        super(msgs.getString("title"));
    }

    /**
     * Get instance of class.
     * @return the instance of class
     */
    public static CatalogForm getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new CatalogForm();
            INSTANCE.init();
        }

        return INSTANCE;
    }

    /**
     * Init form.
     */
    private void init() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("icons/VideoCatalog.jpg")));
        setJMenuBar(new MenuBar(this));

        buildMainPanel();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // set minimum size
        setMinimumSize(new Dimension(MINIMUM_WIDTH_SIZE, MINIMUM_HEIGHT_SIZE));
        pack();
        setSize(getMinimumSize());
        setLocationRelativeTo(null);
    }

    private void buildMainPanel() {
        Container formPanel = getContentPane();
        FormLayout layout = new FormLayout("2dlu, fill:pref:grow, 3dlu, 100px, 2dlu",
                "2dlu, fill:pref:grow, 3dlu, bottom:30px, 2dlu");
        formPanel.setLayout(layout);

        tableModel = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tableModel.addColumn("№");
        tableModel.addColumn(msgs.getString("nameColumn"));
        JTable table = new JTable(tableModel);
        table.removeEditor();
        table.getColumnModel().getColumn(0).setMaxWidth(50);
        table.getColumnModel().getColumn(0).setResizable(false);
        table.getColumnModel().getColumn(1).setPreferredWidth(200);
        formPanel.add(new JScrollPane(table), CELL_CONSTRAINTS.xywh(2, 2, 2, 3));

        formPanel.add(getControlPanel(), CELL_CONSTRAINTS.xy(4, 2));

        JButton button = new JButton("Выход");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                INSTANCE.dispose();
            }
        });
        formPanel.add(button, CELL_CONSTRAINTS.xy(4, 4));
    }

    private JPanel getControlPanel() {
        FormLayout layout = new FormLayout("fill:100px", "fill:pref, center:pref, center:pref, fill:pref");
        JPanel panel = new JPanel(layout);

        JButton button = new JButton("Выбрать");
        panel.add(button, CELL_CONSTRAINTS.xy(1, 1));

        button = new JButton("Отменить");
        panel.add(button, CELL_CONSTRAINTS.xy(1, 2));

        return panel;
    }

    public void fillData(List<Film> select) {
        int rowNumber = 0;
        for (Film film : select) {
            tableModel.addRow(new Object[] { ++rowNumber, film.getName() });
        }
    }

}
